﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Laser_HandIO
{
    public partial class Frm_生产日志信息 : Form
    {
        public Frm_生产日志信息(string[] data)
        {
            InitializeComponent();
            for (int i = 0; i < data.Length; i++)
            {
                if (!data[i].Contains('['))
                {
                    lb_Show_Mes.Items.Add(data[i]);
                }
            }
        }

        private void btn_退出_Click(object sender, EventArgs e)
        {
            Close();
        }

        private const int ITEM_PADDING = 13;//各项之间的边距

        private void lb_Show_Mes_DrawItem(object sender, DrawItemEventArgs e)
        {
            int index = e.Index;//获取当前要进行绘制的行的序号，从0开始。
            if (index < 0)
            {
                return;
            }

            ListBox listBox = sender as ListBox;

            e.DrawBackground();//画背景颜色

            e.DrawFocusRectangle();//画聚焦项的边框

            Graphics g = e.Graphics;//获取Graphics对象。

            Rectangle itemBounds = e.Bounds;//获取当前要绘制的行的一个矩形范围。

            //文字绘制的区域，留出一定间隔

            Rectangle textBounds = new Rectangle(itemBounds.X, itemBounds.Y + ITEM_PADDING, itemBounds.Width, itemBounds.Height);

            string text = Convert.ToString(listBox.Items[index]);

            //因为文本可能会非常长，因此要用自绘实现ListBox项目的自动换行

            TextRenderer.DrawText(g, text, e.Font, textBounds, e.ForeColor,

               TextFormatFlags.WordBreak);

            g.DrawRectangle(Pens.BlueViolet, itemBounds);//画每一项的边框，这样清楚分出来各项。  
        }

        private void lb_Show_Mes_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            ListBox listBox = sender as ListBox;

            int index = e.Index;//获取当前要进行绘制的行的序号，从0开始。

            if (index < 0)
            {
                return;
            }
            string text = Convert.ToString(listBox.Items[index]);

            //超范围后自动换行

            System.Drawing.Size size = TextRenderer.MeasureText(text, listBox.Font, listBox.Size, TextFormatFlags.WordBreak);

            e.ItemWidth = size.Width;

            e.ItemHeight = size.Height + ITEM_PADDING * 2;//适当多一点高度，避免太挤
        }

        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            GeneralAPI.CGeneralMethods.ReleaseCapture();
            GeneralAPI.CGeneralMethods.SendMessage(this.Handle, GeneralAPI.CGeneralMethods.WM_SYSCOMMAND, GeneralAPI.CGeneralMethods.SC_MOVE + GeneralAPI.CGeneralMethods.HTCAPTION, 0);
        }
    }
}
